<!--
 * @Author: zengwj
 * @Date: 2022-06-21 10:44:16
 * @LastEditors: zengwj
 * @LastEditTime: 2022-06-22 11:17:08
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function myNew(constructor) {
      // 创建一个新对象 把构造函数的原型挂到新对象底下
      const obj = Object.create(constructor)
      // 获取参数
      const rgas = [...arguments].slice(1)
      // 执行构造函数 改变this 指向为新的对象 获得返回值
      const res = constructor.call(obj, ...rgas)
      // 如果返回值为对象 返回 否则返回新对象
      return res instanceof Object ? res : obj
    }
    function Car(carName) {
      this.carName = carName
    }
    const demo = myNew(Car, 'fuck')
    console.log(demo)
    const demo1 = new Car('fuck')
    console.log(demo1)
  </script>
</body>

</html>